package com.sixtofly.chineseaddressanalyzer.analyzer.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sixtofly.chineseaddressanalyzer.analyzer.AddressDataSource;
import com.sixtofly.chineseaddressanalyzer.entity.Address;
import com.sixtofly.chineseaddressanalyzer.entity.Province;
import com.sixtofly.chineseaddressanalyzer.entity.dto.AddressAnalyzeParams;
import com.sixtofly.chineseaddressanalyzer.mapper.AddressMapper;
import com.sixtofly.chineseaddressanalyzer.mapper.ProvinceMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author LiDaShuai
 * @date 2021-01-13 12:04
 * @description
 */
@Service
public class MysqlAddressDataSource implements AddressDataSource {


    private final AddressMapper addressMapper;
    private final ProvinceMapper provinceMapper;

    public MysqlAddressDataSource(AddressMapper addressMapper, ProvinceMapper provinceMapper) {
        this.addressMapper = addressMapper;
        this.provinceMapper = provinceMapper;
    }

    @Override
    public List<Address> findByNameLike(AddressAnalyzeParams params) {
        return addressMapper.findByNameLike(params);
    }
    @Override
    public List<Province> findNameByLike(AddressAnalyzeParams params) {
        LambdaQueryWrapper<Province> eq = new LambdaQueryWrapper<Province>().likeRight(Province::getName, params.getName());
        if (params.getLevel()==null){
            eq.eq(Province::getCity,0);
        }else{
            eq.ne(Province::getProvince,params.getLevel());
        }
        return provinceMapper.selectList(eq);
    }
}
